package com.recharge.domain.context;

import java.io.*;


/**
 * shiro 序列化 工具 （暂时没用）
 */
public class RedisMessageSerializer {


    /**
     * 对象序列化成字符串
     */
    public static byte[] serialize(Object obj) {
        ByteArrayOutputStream byteArrayOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        byte[] buffer = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(obj);
            buffer = byteArrayOutputStream.toByteArray();
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                if(objectOutputStream != null) {
                    objectOutputStream.close();
                }
                if(byteArrayOutputStream != null) {
                    byteArrayOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return buffer;
    }


    /**
     *
     * 字符串序列化成Javabean
     */
    public static Object deserialize(byte[] data)  {
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectInputStream objectInputStream = null;
        Object object = null;
        try{
            byteArrayInputStream = new ByteArrayInputStream(data);
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            object = objectInputStream.readObject();
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            try {
                if(byteArrayInputStream != null) {
                    byteArrayInputStream.close();
                }
                if(objectInputStream != null) {
                    objectInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return object;

    }
}
